<template>
  <el-drawer
    :title="title"
    :visible.sync="dialogFormVisible"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="140px"
      :model="form"
      :rules="rules"
    >
      <el-form-item label="姓名">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="登录账号" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="登录密码" prop="password">
        <el-input v-model="form.password" />
      </el-form-item>
      <el-form-item label="录入人">
        <el-input v-model="form.lururen" />
      </el-form-item>
      <el-form-item label="学习平台">
        <el-select v-model="form.schoolCode" clearable placeholder="请选择">
              <el-option
                v-for="item in schoolList"
                :key="item.id"
                :label="item.school_name"
                :value="item.id"
              />
            </el-select>
      </el-form-item>
      <!-- 是否做任务 -->
        <el-form-item label="是否做任务">
          <el-radio-group v-model="form.isTask">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      <el-form-item>
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<script>
  import { doEdit } from '@/api/unionMember'

  export default {
    name: 'UnionMemberForm',
    props: {
      schoolList: {
        type: Array,
        default: () => []
      }
    },
    data() {
      return {
        form: {
          isTask: "1",
        },
        rules: {
          username: [{ required: true, trigger: 'blur', message: '请输入登录账号' }],
          password: [{ required: true, trigger: 'blur', message: '请输入登录密码' }],
        },
        title: '',
        
        dialogFormVisible: false,
      }
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = JSON.parse(JSON.stringify(Object.assign({}, row)))
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            console.log(this.form);
            const { msg } = await doEdit({
              name: this.form.name,
              id: this.form.id,
              username: this.form.username,
              password: this.form.password,
              schoolCode: this.form.schoolCode,
              lururen: this.form.lururen,
            })
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            this.$emit('fetch-data')
            this.close()
          }
        })
      },
    },
  }
</script>
